<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>LayUI 风格登录页</title>
    <!-- LayUI CSS -->
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <style>
        .login-container {
            background: url('https://example.com/bg.jpg') no-repeat center;
            background-size: cover;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .login-box {
            width: 380px;
            padding: 30px;
            background: rgba(255,255,255,0.9);
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body>
<div class="login-container">
    <div class="login-box">
        <form class="layui-form"  method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username"
                           lay-verify="required"
                           placeholder="请输入用户名"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-block">
                    <input type="password" name="password"
                           lay-verify="required|pass"
                           placeholder="请输入密码"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-fluid"
                            lay-submit lay-filter="login">立即登录</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- LayUI JS -->
<script th:src="@{/static/layui/layui.js}"></script>
<script>
    layui.use(['form', 'layer','jquery'], function(){
        const form = layui.form;
        const layer = layui.layer;
        const $ = layui.$;

        // 自定义验证规则
        form.verify({
            pass: [/^[\S]{1,12}$/,'密码必须6到12位且不能包含空格']
        });

        // 监听提交
        form.on('submit(login)', function(data){
            // 此处可添加AJAX登录逻辑
            // 发送 AJAX 请求
            $.ajax({
                url: '/idea/user/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function (response) {
                    if (response.code==200) {
                        layer.msg('登录成功', { icon: 1 });
                        setTimeout(function () {
                            window.location.href = '/home'; // 跳转到主页
                        }, 1000);
                    } else {
                        layer.msg("登录失败", { icon: 2 });
                    }
                },
                error: function () {
                    layer.msg('请求失败，请稍后重试', { icon: 2 });
                }
            });
            return false; // 阻止表单自动提交
        });
    });
</script>
</body>
</html>
